const crypto = require('crypto');

module.exports = {
  encryptIV({ plain, key, algorithm = 'aes-128-cbc', iv = Buffer.from("0000000000000000") }) {
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let crypted = cipher.update(plain, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted
  },
  decryptIV({ encrypted, key, algorithm = 'aes-128-cbc', iv = Buffer.from("0000000000000000") }) {
    var decipher = crypto.createDecipheriv(algorithm, key, iv);

    let decrypted = decipher.update(Buffer.from(encrypted, 'hex'));
    decrypted += decipher.final('utf8')
    return decrypted
  },
};

const phone = '18812341234'
const key = 'fb9b3857531e0d89'

const encrypted = module.exports.encryptIV({ plain: phone, key })
const decrypted = module.exports.decryptIV({ encrypted, key })

// output da2e9df28673165000312dfb15f57517
console.log(encrypted)

// output 18812341234
console.log(decrypted)
